// web/src/app/api/(social)/workspaces/route.ts
import {NextRequest, NextResponse} from "next/server";
import {db} from "@/lib/db";

export async function GET(req: NextRequest) {
    const searchParams = req.nextUrl.searchParams;
    let userId = (searchParams.get('userId'))
    let joinCode = (searchParams.get('joinCode'))

    let where = {}
    // @ts-ignore
    if (userId) where.userId = userId
    // @ts-ignore
    if (joinCode) where.joinCode = joinCode
    const workspaces = await db.workspace.findMany({
        where
    })

    return NextResponse.json(workspaces)
}

export async function POST(req:NextRequest){
    try {
        const data = await req.json()
        const workspace = await db.workspace.create({
            data
        })
        return NextResponse.json({workspace})
    } catch (err) {
        console.error(err)
        return NextResponse.json({err})
    }
}